/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import entities.*;
import org.jgap.*;
import org.jgap.impl.*;
import java.util.*;

/**
 *
 * @author Acer
 */
public class Printing {

    public static String printChromosome(Domain domain, List<Query> queries, IChromosome chromosome) {
        String str = "";
        int i = 0;
        for (Query q : queries) {
            str += "query" + ++i + ": " + q.toString() + "\n";
        }
        str += "\n\nMaterialized Views: chromosome length = " + chromosome.getGenes().length + "\n";
        i = 0;
        int j = 0;
        for (ViewGene vg : domain.getViewGraph().getAllViews()) {
            j++;
            BooleanGene isViewMat = (BooleanGene) chromosome.getGene(vg.getTableIndex());
            if (isViewMat.booleanValue() == true) {
                str += ++i + " View:" + j + " " + vg.toString() + " \n\tAA: ";// .split(">")[0] + ">"
                for (Table dimRelation : vg.getFKRelations()) {
                    // forall relations in dimension
                    for (Table rel : dimRelation) {
                        // +1 for the PK
                        int start = vg.getTableIndex() + rel.getTableIndex();
                        int end = vg.getTableIndex() + dimRelation.getTableIndex() + rel.getDescriptiveAtributes().size();
                        // The root relation does not have an index for the PK
                        if (dimRelation.equals(rel)) {
                            end--;
                        }
                        for (int k = start; k < end; k++) {
                            if (((BooleanGene) chromosome.getGene(k)).booleanValue() == true) {
                                // The root relation does not have an index for the PK
                                if (k == start && !dimRelation.equals(rel)) {
                                    str += "PK" + rel.getName() + " ";
                                } else {
                                    // The root relation does not have an index for the PK
                                    if (dimRelation.equals(rel)) {
                                        str += rel.getDescriptiveAtributes().get(k - start).getName() + " ";
                                    } else {
                                        str += rel.getDescriptiveAtributes().get(k - start - 1).getName() + " ";
                                    }
                                }
                            }
                        }

                    }
                }
                str += "\n";
            } // end isMaterialized
        }
        return str;
    }

    public static String printViews(Domain domain) {
        String str = "";
        str += "root:" + domain.getViewGraph().getRoot() + "\n";
        int i = 0;
        for (ViewGene vg : domain.getViewGraph().getAllViews()) {
            str += i++ + ": view:" + vg + "\n";
        }
        return str;
    }

    public static String printMatViews(Domain domain) {
        String str = "";
        int i = 0;
        for (Table view : domain.getMaterializedViews().values()) {
            str += i++ + ": view:" + view + "\n";
        }
        return str;
    }

    public static String printRelations(Domain domain) {
        String str = "";
        for (Table dimRelation : domain.getViewGraph().getRoot().getFKRelations()) {
            for (Table table : dimRelation) {
                str += "\n" + table;
            }
        }
        str = str.substring(0, str.length() - 1);
        return str;
    }
}

